package com.design.prototypepattern.example;

import java.lang.reflect.Field;

/**
 * @auther LiZiBa
 * @date 2020/8/8 23:20
 * @description:
 **/
public class BeanUtil {

    public static Object copy(Object copyObject){

        Class<?> clazz = copyObject.getClass();

        Object returnValue = null;
        try {
            returnValue = clazz.newInstance();

            for(Field field : clazz.getDeclaredFields()){
                // 取消 Java 语言访问检查
                field.setAccessible(true);
                field.set(returnValue, field.get(copyObject));
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return returnValue;
    }
}
